/* global angular */
'use strict'

angular.module('myApp.viewLogin', ['ngRoute'])

    .controller("ViewLogin", ['$scope', '$http', '$location', function ($scope, $http, $location) {
        $scope.userName;
        $scope.password;

        if (undefined == $scope.users) {
            $http.get('json/users.json').success(function (data) {
                $scope.users = data;
            });
        }

        $scope.onLogin = function () {
            var valid = false;
            for (var i = 0; i < $scope.users.length; ++i) {
                if ($scope.users[i].user_name == $scope.userName
                    && $scope.users[i].password == $scope.password) {
                    valid = true;
                    break;
                }
            }

            if (valid) {
                $location.path('/' + $scope.userName + '/tasklist');
            } else {
                alert("Invalid user name or password!");
            }
        };

    }]);